// run: $exec < input
#include <iostream>
#include <string>
#include <map>
#include <set>

std::string get_before(std::string const & s, char ch)
{
	if (s.find(ch) == std::string::npos) return s;
	return s.substr(0, s.find(ch));
}

std::string get_after(std::string const & s, char ch)
{
	if (s.find(ch) == std::string::npos) return "";
	return s.substr(s.find(ch) + 1);
}

int main()
{
	std::ios::sync_with_stdio(false);
	int T; std::cin >> T;
	std::string s;
	std::string t;
//	std::cout << get_after("lkasdf_lkaf123lzakjf:lakjdsf__13asdf", ':') << '\n'; return 0;
	while (T--) {
		std::set<std::string> defined;
		std::map<std::string, std::set<std::string> > method;
		std::map<std::string, std::string> father;
		while (std::cin >> s && s != "end") {
			if (s == "begin") continue;
			std::cin >> t;
			if (s == "class") { // class
				std::string before = get_before(t, ':');
				std::string after = get_after(t, ':');
				if (defined.find(before) != defined.end()) {
					std::cout << "oops!\n";
					continue;
				}
				if (!after.empty()) {
					if (defined.find(after) == defined.end()) {
						std::cout << "oops!\n";
						continue;
					}
					father[before] = after;
				}
				defined.insert(before);
				std::cout << "class " << t << '\n';
			} else if (s == "def") { // def
				std::string before = get_before(t, '.');
				std::string after = get_after(t, '.');
				if (defined.find(before) == defined.end()) {
					std::cout << "oops!\n";
					continue;
				}
				if (method[before].find(after) != method[before].end()) {
					std::cout << "redef " << t << '\n';
					continue;
				}
				method[before].insert(after);
				std::cout << "def " << t << '\n';
			} else if (s == "undef") { // undef
				std::string before = get_before(t, '.');
				std::string after = get_after(t, '.');
				if (defined.find(before) == defined.end()) {
					std::cout << "oops!\n";
					continue;
				}
				if (method[before].find(after) == method[before].end()) {
					std::cout << "oops!\n";
					continue;
				}
				method[before].erase(after);
				std::cout << "undef " << t << '\n';
			} else if (s == "call") { // call
				std::string before = get_before(t, '.');
				std::string after = get_after(t, '.');
				if (defined.find(before) == defined.end()) {
					std::cout << "oops!\n";
					continue;
				}
				std::string tmp = before;
				while (!tmp.empty() && method[tmp].find(after) == method[tmp].end())
					tmp = father[tmp];
				if (tmp.empty())
					std::cout << "oops!\n";
				else
					std::cout << "invoke " << tmp << '.' << after << '\n';
			}
		}
		std::cout << '\n';
	}
}

